package com.xigua.a03_bean生命周期;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * @author LiRongHua
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: ${todo}
 * @date 2022/4/116:47
 */
@Component
@Slf4j
public class MyBeanPostProcessor implements InstantiationAwareBeanPostProcessor ,DestructionAwareBeanPostProcessor{

    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        if (beanName.equals("lifeCyclBean")){
            log.debug("实例化之前执行 --返回的bean会替换掉原来的bean" );
        }
        return null;
    }

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        if (beanName.equals("lifeCyclBean")){
            log.debug("实例化之后执行");
        }
        return true;
    }

    @Override
    public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
        if (beanName.equals("lifeCyclBean")){
            log.debug("依赖注入阶段执行 如@service @Autowired @Resource  @Value");
        }
        return pvs;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (beanName.equals("lifeCyclBean")){
            log.debug("初始化之前执行 这里返回的对象会替换掉原来的bean 如 @postConstruct,@ConfigurationProperties");
        }
        return null;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (beanName.equals("lifeCyclBean")){
            log.debug("初始化之后执行 这里返回的对象会替换掉原来的bean 如 代理增强");
        }
        return null;
    }

    @Override
    public void postProcessBeforeDestruction(Object o, String s) throws BeansException {
        if (s.equals("lifeCyclBean")){
            log.debug("销毁之前执行 如 @PreDestory");
        }
    }
}
